<template>
	<div>
		<el-dialog 
      title="风险活动信息"
      append-to-body
      :visible.sync="riskActivityDialog"
      :close-on-click-modal="false" 
      :before-close="handleCloseRiskActivity"
      width="60%"
    >
      <el-form :model="riskActivityForm" :rules="riskActivityRules">

        <!-- 引导词、要素 -->
        <div class="basic_div">
          <el-row>

            <!-- 引导词 -->
            <el-col :span="12">
              <el-form-item label="引导词" prop="introduceValue">
                <div class="introduce_value">
                  <el-select 
                    v-model="riskActivityForm.introduceValue" 
                    clearable 
                    placeholder="请选择引导词"
                    size="small"
                  >
                    <el-option
                      v-for="item in introducers"
                      :key="item.dictKey"
                      :label="item.dictValue"
                      :value="item.dictKey">
                    </el-option>
                  </el-select>
                </div>
              </el-form-item>
            </el-col>

            <!-- 要素 -->
            <el-col :span="12">
              <el-form-item label="要素" prop="essentialFactor">
                <div class="essential_factor">
                  <el-input
                    v-model="riskActivityForm.essentialFactor"
                    placeholder="请输入要素"
                    size="small">
                  </el-input>
                </div>
              </el-form-item>
            </el-col>

          </el-row>
        </div>

        <!-- 偏差 -->
        <div class="deviation_div">
          <el-row>
            <el-col :span="24">
              <el-form-item label="偏差" prop="deviation">
                <el-input
                  type="textarea"
                  :rows="3"
                  placeholder="请输入偏差"
                  v-model="riskActivityForm.deviation">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 可能原因 -->
        <div class="possible_reason">
          <el-row>
            <el-col :span="24">
              <el-form-item label="可能原因" prop="possibleReason">
                <el-input
                  type="textarea"
                  :rows="3"
                  placeholder="请输入可能原因"
                  v-model="riskActivityForm.possibleReason">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 后果 -->
        <div class="consequences_div">
          <el-row>
            <el-col :span="24">
              <el-form-item label="后果" prop="consequences">
                <el-input
                  type="textarea"
                  :rows="3"
                  placeholder="请输入后果"
                  size="small"
                  v-model="riskActivityForm.consequences">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 安全措施 -->
        <div class="safe_measure">
          <el-row>
            <el-col :span="24">
              <el-form-item label="安全措施" prop="safeMeasure">
                <el-input
                  type="textarea"
                  :rows="3"
                  placeholder="请输入安全措施"
                  size="small"
                  v-model="riskActivityForm.safeMeasure">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 注释 -->
        <div class="notes_div">
          <el-row>
            <el-col :span="24">
              <el-form-item label="注释" prop="notes">
                <el-input
                  type="textarea"
                  :rows="3"
                  placeholder="请输入注释"
                  size="small"
                  v-model="riskActivityForm.notes">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 建议安全措施 -->
        <div class="advice_safe_measure">
          <el-row>
            <el-col :span="24">
              <el-form-item label="建议安全措施" prop="adviceSafeMeasure">
                <el-input
                  type="textarea"
                  :rows="3"
                  placeholder="请输入建议安全措施"
                  size="small"
                  v-model="riskActivityForm.adviceSafeMeasure">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 执行人 -->
        <div class="executor_div">
          <el-row>
            <el-col :span="24">
              <el-form-item label="执行人" prop="executor">
                <el-select 
                  v-model="riskActivityForm.executor" 
                  clearable 
                  placeholder="请选择执行人"
                  size="small"
                >
                  <el-option
                    v-for="item in executors"
                    :key="item.id"
                    :label="item.realName"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- 按钮 -->
        <div class="risk_activity_footer">
          <el-button 
            type="primary"
            size="small"
            @click="saveRiskActivity"
            :loading="loadSaveRiskActivity">
            确 定
          </el-button>
          <el-button 
            size="small"
            @click="cancelSaveRiskActivity">
            取 消
          </el-button>
        </div>

      </el-form>
    </el-dialog>
	</div>
</template>

<script>
import { getIntroducers, getExecutors } from "@/api/saferisk/hazopriskanalysis";

export default {
  props: {
    riskActivityDialog: {default: false},
    activityType: {default: "ADD"},
    aIndex: {default: ""},
    selectedActivity: {default: {}}
  },
  data() {
    return {
      riskActivityForm: {
        aIndex: "",
        introduceValue: "",
        essentialFactor: "",
        deviation: "",
        possibleReason: "",
        consequences: "",
        safeMeasure: "",
        notes: "",
        adviceSafeMeasure: "",
        executor: ""
      },
      riskActivityRules: {
        introduceValue: [{required: true, message: '请选择引导词', trigger: 'change'}],
      },
      loadSaveRiskActivity: false,
      introducers: [],
      executors: []
    }
  },
  watch: {
    riskActivityDialog(){
      if(this.riskActivityDialog === true){
        this.initData();
      }
    }
  },
  mounted() {
    this.getIntroducerList();
    this.getExecutorList();
  },
  methods: {

    // 获取引导词列表
    getIntroducerList() {
      getIntroducers().then(res => {
        this.introducers = res.data.data;
      });
    },

    // 获取执行人列表
    getExecutorList() {
      getExecutors().then(res => {
        this.executors = res.data.data;
      });
    },

    // 初始化弹窗
    initData() {
      if(this.activityType === "ADD") {
        this.riskActivityForm = {
          introduceValue: "",
          essentialFactor: "",
          deviation: "",
          possibleReason: "",
          consequences: "",
          safeMeasure: "",
          notes: "",
          adviceSafeMeasure: "",
          executor: "",
          aIndex: ""
        };
      } else if(this.activityType === "UPDATE") {
        let row = this.selectedActivity;
        this.riskActivityForm = {
          introduceValue: row.ydc,
          essentialFactor: row.ys,
          deviation: row.pc,
          possibleReason: row.knyy,
          consequences: row.hg,
          safeMeasure: row.aqcs,
          notes: row.zs,
          adviceSafeMeasure: row.jyaqcs,
          executor: row.zxr,
          aIndex: this.aIndex
        };
      }
    },

    // 保存风险活动
    saveRiskActivity() {
      var activity = this.riskActivityForm;
      if(activity.introduceValue === "") {
        this.$message.warning("请选择引导词!");
        return;
      }
      var insertData = {
        ydc: activity.introduceValue,
        ys: activity.essentialFactor === "" ? "" : activity.essentialFactor,
        pc: activity.deviation === "" ? "" : activity.deviation,
        knyy: activity.possibleReason === "" ? "" : activity.possibleReason,
        hg: activity.consequences === "" ? "" : activity.consequences,
        aqcs: activity.safeMeasure === "" ? "" : activity.safeMeasure,
        zs: activity.notes === "" ? "" : activity.notes,
        jyaqcs: activity.adviceSafeMeasure === "" ? "" : activity.adviceSafeMeasure,
        zxr: activity.executor === "" ? "" : activity.executor,
      };
      this.$emit('saveRiskActivity', insertData);
    },
    
    // 取消保存风险活动
    cancelSaveRiskActivity() {
      this.$emit('cancelSaveRiskActivity');
    },

    // 关闭弹窗
    handleCloseRiskActivity(done) {
      done();
      this.$emit('handleCloseRiskActivity');
    }

  }
}
</script>